package com.example.demo.controller;

import com.example.demo.bean.Student;
import com.example.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    // 查询所有学生
    @GetMapping
    public List<Student> getAllStudents() {
        return studentService.list();
    }

    // 根据ID查询学生
    @GetMapping("/{id}")
    public Student getStudentById(@PathVariable Integer id) {
        return studentService.getById(id);
    }

    // 添加学生
    @PostMapping
    public void addStudent(@RequestBody Student student) {
        studentService.save(student);
    }

    // 更新学生
    @PutMapping("/{id}")
    public void updateStudent(@PathVariable Integer id, @RequestBody Student student) {
        student.setId(id);
        studentService.updateById(student);
    }

    // 删除学生
    @DeleteMapping("/{id}")
    public void deleteStudent(@PathVariable Integer id) {
        studentService.removeById(id);
    }
}
